package com.sino.androiddesign.customView

import android.content.Context
import android.graphics.*
import android.os.Bundle
import android.support.v4.app.Fragment
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup

import com.sino.androiddesign.R
import kotlinx.android.synthetic.main.fragment_demo5_bezier_curve.*


/**
 * 贝塞尔曲线
 *
 */
class Demo5BezierCurveFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_demo5_bezier_curve, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        control1.setOnClickListener {
            three_view.setMode(true)
        }

        control2.setOnClickListener {

            three_view.setMode(false)
        }

    }

}

//二阶贝塞尔曲线
class BezierCurveView : View {
    constructor(context: Context) : this(context, null)
    constructor(context: Context, attributeSet: AttributeSet?) : super(context, attributeSet)


    private val helpPaint = Paint()

    private val mPaint: Paint = Paint()

    private val start = PointF(0f, 0f)
    private val end = PointF(0f, 0f)
    private val control = PointF(0f, 0f)

    private var centerX: Float = 0f
    private var centerY: Float = 0f

    private val mPath = Path()

    init {
        mPaint.color = Color.BLACK
        mPaint.strokeWidth = 8f
        mPaint.style = Paint.Style.STROKE
        mPaint.textSize = 60f

        helpPaint.color = Color.RED
        helpPaint.style = Paint.Style.STROKE
        helpPaint.strokeWidth = 4f
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        centerX = w / 2.toFloat()
        centerY = h / 2.toFloat()

        start.x = centerX - 200
        start.y = centerY
        end.x = centerX + 200
        end.y = centerY
        control.x = centerX
        control.y = centerY - 100

    }


    override fun onTouchEvent(event: MotionEvent?): Boolean {
        mPath.reset()
        control.x = event!!.x
        control.y = event.y
        postInvalidate()
        return true
    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        canvas!!.drawPoint(start.x, start.y, helpPaint)
        canvas.drawPoint(control.x, control.y, helpPaint)
        canvas.drawPoint(end.x, end.y, helpPaint)

        canvas.drawLine(start.x, start.y, control.x, control.y, helpPaint)
        canvas.drawLine(end.x, end.y, control.x, control.y, helpPaint)


        mPath.moveTo(start.x, start.y)
        mPath.quadTo(control.x, control.y, end.x, end.y)

        canvas.drawPath(mPath, mPaint)

    }
}


//三阶贝塞尔曲线
//由两个控制点和数据点 构成

class ThreeBezuerCurveView : View {
    constructor(context: Context) : this(context, null)

    constructor(context: Context, attributeSet: AttributeSet?) : super(context, attributeSet)

    private val mPaint: Paint = Paint()

    private val start = PointF(0f, 0f)
    private val end = PointF(0f, 0f)

    //    控制点
    private val control1 = PointF(0f, 0f)
    private val control2 = PointF(0f, 0f)

    //中心点
    private var centerX: Int? = null
    private var centerY: Int? = null


    private var mode: Boolean = true

    private val mPath:Path = Path()

    init {
        mPaint.strokeWidth = 8f
        mPaint.color = Color.BLACK
        mPaint.style = Paint.Style.STROKE
        mPaint.textSize = 60f

    }

    fun setMode(mode: Boolean) {
        this.mode = mode
        mPath.reset()
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        centerX = w / 2
        centerY = h / 2


        start.x = (centerX!! - 200).toFloat()
        start.x = centerY!!.toFloat()

        end.x = (centerX!! + 200).toFloat()
        end.y = centerY!!.toFloat()

        control1.x = centerX!!.toFloat()
        control1.y = (centerY!! - 100).toFloat()

        control2.x = centerX!!.toFloat()
        control2.y = (centerY!! - 100).toFloat()

    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
        if (mode) {
            control1.x = event!!.x
            control1.y =event.y
        }else{
            control2.x =event!!.x
            control2.y = event.y
        }
        mPath.reset()
        postInvalidate()
        return super.onTouchEvent(event)
    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)


        mPaint.color= Color.GRAY
        mPaint.strokeWidth =20f

        canvas!!.drawPoint(start.x,start.y,mPaint)
        canvas.drawPoint(end.x,end.y,mPaint)
        canvas.drawPoint(control1.x,control1.y,mPaint)
        canvas.drawPoint(control2.x,control2.y,mPaint)


        mPaint.strokeWidth = 4f;
        canvas.drawLine(start.x, start.y, control1.x, control1.y, mPaint);
        canvas.drawLine(control1.x, control1.y,control2.x, control2.y, mPaint);
        canvas.drawLine(control2.x, control2.y,end.x, end.y, mPaint);

        mPaint.color = Color.RED
        mPaint.strokeWidth =8f

        mPath.moveTo(start.x,start.y)

        mPath.cubicTo(control1.x,control1.y,control2.x,control2.y,end.x,end.y)

        canvas.drawPath(mPath,mPaint)



    }

}
